/* * Copyright (c) 2013, EPFL * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of the EPFL nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ package net.sf.orcc.simulators.runtime.std.audio.impl; import java.math.BigInteger; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.LineUnavailableException; import javax.sound.sampled.SourceDataLine; /** * Audio native functions for the simulator * * @author Malgorzata Wiszniewska * */ public class Audio { private static final int BUFFER_SIZE = 128000; private static AudioFormat audioFormat; private static SourceDataLine line = null; private static DataLine.Info info; private static byte[] bdata = new byte[BUFFER_SIZE]; private static int nBytesRead = 0; public static boolean audio_bufferFull() { if (nBytesRead == BUFFER_SIZE) { return true; } else { return false; } } public static void audio_close() { line.drain(); line.close(); } public static void audio_initAudioFormat(BigInteger SampleRate, BigInteger SampleSizeInBits, BigInteger Channels) { if (SampleSizeInBits.intValue() == 8) { audioFormat = new AudioFormat(SampleRate.floatValue(), SampleSizeInBits.intValue(), Channels.intValue(), false, false); } else { audioFormat = new AudioFormat(SampleRate.floatValue(), SampleSizeInBits.intValue(), Channels.intValue(), true, false); } info = new DataLine.Info(SourceDataLine.class, audioFormat); if (AudioSystem.isLineSupported(info)) { try { line = (SourceDataLine) AudioSystem.getLine(info); line.open(audioFormat); } catch (LineUnavailableException e) { e.printStackTrace(); System.exit(1); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } else { System.out .println("Format of the file is incorrect (only 8 or 16 bits per sample are supported)."); } } public static void audio_play() { int increment = ((nBytesRead % 4) == 0) ? 0 : 4 - (nBytesRead % 4); for (int i = 0; i < increment; i++) { bdata[nBytesRead + i] = (byte) 0; } line.start(); line.write(bdata, 0, nBytesRead + increment); nBytesRead = 0; bdata = new byte[BUFFER_SIZE]; } public static void audio_receive(BigInteger data) { bdata[nBytesRead] = data.byteValue(); nBytesRead++; } }